<?php

namespace App\Admin\Controllers\Common;

use App\Common\Models\Question;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Validate\Common\Question as ValidateCommonQuestion;
class QuestionController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = Question::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = Question::where(['id' => $id,'is_del'=>1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['question_title', 'question_answer']);

        $Validate = new ValidateCommonQuestion();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $data = $req_data;
        $data['create_at'] = time();
        $data['update_at'] = time();
        Question::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id','question_title', 'question_answer']);

        $Validate = new ValidateCommonQuestion('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        Question::where(['id'=>$req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 发送
     */
    public function dosend(Request $request)
    {
        $id = $request->id ?? 0;
        $info = Question::where([['id', $id],['is_del',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        if($info->is_send!=1){
            return response()->json(['code' => '001', 'msg' => '已发送，不可重复操作']);
        }
        Question::where([['id', $id]])->update(['is_send' => 2, 'send_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $info = Question::where([['id', $id],['is_del',1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        Question::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}